﻿using System;
using System.Collections.Specialized;
using System.Diagnostics;

namespace SSG.Tools
{
    /// <summary>
    ///   Provides a trace switch, which can also be set externally.
    /// </summary>
    public class ToolsTraceSwitch
    {
        #region Fields / Properties
        /// <summary>
        ///   Trace switch, the name must correspond with entry in App.config
        /// </summary>
        private static TraceSwitch _traceSwitch = null;
        /// <summary>
        ///   <see cref = "Switch.Attributes" />.
        /// </summary>
        public static StringDictionary Attributes { get { return _traceSwitch.Attributes; } }
        /// <summary>
        ///   <see cref = "Switch.DisplayName" />.
        /// </summary>
        public static string DisplayName { get { return _traceSwitch.DisplayName; } }
        /// <summary>
        ///   <see cref = "Switch.Description" />.
        /// </summary>
        public static string Description { get { return _traceSwitch.Description; } }
        /// <summary>
        ///   <see cref = "System.Diagnostics.TraceSwitch.Level" />.
        /// </summary>
        public static TraceLevel Level { get { return _traceSwitch.Level; } set { _traceSwitch.Level = value; } }
        /// <summary>
        ///   <see cref = "System.Diagnostics.TraceSwitch.TraceError" />.
        /// </summary>
        public static bool TraceError { get { return _traceSwitch.TraceError; } }
        /// <summary>
        ///   <see cref = "System.Diagnostics.TraceSwitch.TraceWarning" />.
        /// </summary>
        public static bool TraceWarning { get { return _traceSwitch.TraceWarning; } }
        /// <summary>
        ///   <see cref = "System.Diagnostics.TraceSwitch.TraceInfo" />.
        /// </summary>
        public static bool TraceInfo { get { return _traceSwitch.TraceInfo; } }
        /// <summary>
        ///   <see cref = "System.Diagnostics.TraceSwitch.TraceVerbose" />.
        /// </summary>
        public static bool TraceVerbose { get { return _traceSwitch.TraceVerbose; } }
        /// <summary>
        ///   Underlying trace switch.
        /// </summary>
        public static TraceSwitch TraceSwitch
        {
            get
            {
                Init();
                return ToolsTraceSwitch._traceSwitch;
            }
            set
            {
                if (value == null) throw new ArgumentException("value");
                ToolsTraceSwitch._traceSwitch = value;
                Trace.WriteLineIf(ToolsTraceSwitch._traceSwitch.TraceVerbose, typeof (ToolsTraceSwitch).GetClassname() + " registered switch " + ToolsTraceSwitch._traceSwitch.DisplayName + " " + ToolsTraceSwitch._traceSwitch.Level, typeof (ToolsTraceSwitch).Namespace);
            }
        }
        #endregion Fields / Properties

        #region Constructors
        /// <summary>
        ///   Init, e.g. required when used in designer
        /// </summary>
        static ToolsTraceSwitch()
        {
            Init();
        }
        #endregion Constructors

        #region Methods
        /// <summary>
        ///   Init.
        /// </summary>
        private static void Init()
        {
            if (ToolsTraceSwitch._traceSwitch != null) return;
            ToolsTraceSwitch._traceSwitch = new TraceSwitch("SSG Tools TraceSwitch", "Switch in App.config file for " + typeof (ToolsTraceSwitch).GetClassname());
        }
        #endregion Methods
    }
}